<template>
	<view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		onLoad(e) {
			const that = this
			if (!this.isWechat()){
				uni.showModal({
				    title: '提示',
				    content: '请在微信浏览器里面打开',
				    success: function (res) {
				        that.$myRouter.push({name:'index/index'})
				    }
				});
				return
			}
			uni.showLoading({
			    title: '加载中'
			});
		    let code = this.getUrlCode('code')
		    console.log(code)
		    if(code !== null || code !== "") {
		        this.wxAuthorize(code)
		    }
		},
		methods: {
			getUrlCode (name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
			},
			isWechat(){
				return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
			},
			wxAuthorize(code) {
				const that = this
			    if (code) {  
					that.$api.home.wechatLogin({code:code}).then(res => {
						uni.hideLoading()
						that.$store.dispatch('setMemberLogin',res)
						setTimeout(function() {
							that.$u.toast('登陆成功')
							setTimeout(function() {
								location.replace('/')
								// that.$myRouter.push({name:'index/index'})
							}, 1500);
						}, 500);
					}).catch(err => {
						uni.hideLoading()
						setTimeout(function() {
							that.$u.toast(err.message)
						}, 500);
					})
			    }else {
					that.$api.home.wechatInfo({uri:encodeURIComponent(window.location.href),scope:'snsapi_userinfo'}).then(res => {
						window.location.href = res.url;
					}).catch(err => {
						uni.hideLoading()
						setTimeout(function() {
							that.$u.toast(err.message)
						}, 500);
					})
			    }  
			}
		}
	}
</script>

<style>

</style>
